non_package_files/Examples___/lp_info via labelling (idea).R

# [not yet: idea of how to get lp info via labelling in PATCHMASTER  ]
# how to read labels from PTCHMASTER Files

#  this is based on a .dat file with compound name as exp label
#[#  conc info is by labels in ser and/or sweep]
#[#  for conc we find the latest label on one of these and take at as the current conc]
#[# see function "lastlabel" in get_all  (not yet active)]

# first, to do it proper, clean up:  
# is it correct how we do now unique exp/ser/swp names ? 
#   - at least for expnames, we number them, but there are also fields in the HEKAdata for that
#   - labels overwrite expnames, but they are still in the Tree file, what to do with this ? 
#   - HEKA has a number (groupnumber?)  visible right next to the label, what to do with it ? use for unique name ? 

#   Todo 1) have a look at HEKA-PM and  make example file, then decide
#                 - is Expnumber / Groupnumber always unique ? (could replace our sequential number)
#                   ! no its fishy. maybe a combination of group&exp might be, but lets not rely on. 
#                   ! we should have expnum, groupnum, and grouplabel (cleaned from the dummy) on the list. 
#                   ! we shall have a uniqe name for the tree containing all 3
#                   !!!! warning !!! sweep names are not unique yet, this will craqsh if we set sweeplabels
#                   !!!! warning !!! same with trace names 

#   Todo 2) make decision what to read and how to store in the tree
#                   ! we can store a labels that  are not dummies without bloating the tree
#                   ! we can store all texts without bloating the tree 
#                   ? store everything in the file or also in the tree ? 
#                       - keep tree as small as possible for shiny performance - is this relevant at all ? 
#                       - if everything is in the .dat file (e.g. params as text), the tree is just names and pointers
#                                  it meay even be incomplete ( and deeper levels lazy loaded)
#                       - everything in .data file is very limited but transparent to PM user
#                       - no we cant: we want at least  st_selected and st_checked to be stored (PM's marked attribute is too limited) is t)
#                       - if we store things in the tree we loose meta information if we do getSeries(tree[[c(1,2,3)]])
#                            - the path attribute does not help with this - what is sit actually good for ? 
#                                  - we just use it to extract label info out of the node name? 
#                                  - path is used to identify if a cursor (eventcursor) stored in CURSORS is to be applied to this series
#                       - we do not only need path, we need metdata of the whole family (grandparent-root, parent-experiment and sisters (series of same experiments  ) )
#                                  - sometimes needed to get the history of the cell preceeeding this series
# ==> [ok]   we store path in the tree just for enabling this style: getSeries(tree[[c(1,2,3)]]) 
# ==> [done] we will alow only getSeries(tree, 1,2,3) from now on
# ==> [cleanup] we dont need to store anything that is in the file (Stimnanme, path, label etc) in tree any more
#       - remove label stuff from tree!
# ==> we dont really need the class attribute any more, but maybe its nice to have ? 
#                  
#   3) carefully consider moving things from get_all to when we read the tree or when we do $results()
#   4) make consistent naming 
#            (e.g. now it is  s1$sweeps$Serieslabel and s1$results()$serlabel)
#            we have exp and exp_
#            trace and tracename

# relevant Fields in PM file: 

# SeMark               =   0; (* INT32 *)
# SeLabel              =   4; (* String32Type *)  
# SeComment            =  36; (* String80Type *)  ### also nice to use
# SeSeriesCount        = 116; (* INT32 *)         ### better than our way to number ? always unique ? 
# 
# 
# GrMark               =   0; (* INT32 *)
# GrLabel              =   4; (* String32Size *)
# GrText               =  36; (* String80Size *)  ### also nice to use
# GrExperimentNumber   = 116; (* INT32 *)         ### like E-803 ? 
# GrGroupCount         = 120; (* INT32 *)         ### like the number right to the label in PM GUI ???
# 
# 
# RoRootText           = 120; (* String400Type *) ### also nice to use
  

library(ephys2)
library(magrittr)
labeled<-get_treeinfo("non_package_files/labeled.dat")   #example file

#labeled<-showtree(labeled, level = 5) # see how it looks in GUI

res<-get_all(labeled, "ca simple")     # get_all in this branch automatically extracts labelos
res[,c("exp_", "explabel", "serlabel", "swplabel")]

attributes(labeled[[c(1,1,1)]])

con<-file("non_package_files/labeled.dat", "rb")
attr(labeled[[c(1,1)]], "dataptr")  %>% ephys2:::readlabel(con)              # E-802  ("fake-label")
attr(labeled[[c(1,1)]], "dataptr")  %>% ephys2:::readAny(con,116, "int", 4)  # 802   ->GrExperimentNumber 
attr(labeled[[c(1,1)]], "dataptr")  %>% ephys2:::readAny(con,120, "int", 4)  # 1     ->Groupcount
attr(labeled[[c(1,1,1)]], "dataptr")  %>% ephys2:::readlabel(con)            # "ca-simple", so the fake-label is already here

attr(labeled[[c(1,1)]], "dataptr")  %>% ephys2:::readlabel(con, offset=36)   # ""    ->Exp text ? 


attr(labeled[[c(1,5)]], "dataptr")  %>% ephys2:::readlabel(con)              # Quinidine
attr(labeled[[c(1,5)]], "dataptr")  %>% ephys2:::readAny(con,116, "int", 4)  # 803   ->GrExperimentNumber 
attr(labeled[[c(1,5)]], "dataptr")  %>% ephys2:::readAny(con,120, "int", 4)  # 5     ->Groupcount

close(con)


s1<-getSeries(labeled,1,1,1)

s1$results()$explabel    # from path[2]

s1$sweeps$ser            # path[3]
s1$sweeps$Serieslabel2    # derived from ser
s1$sweeps$Stimulus       # attr(node,"StimulusName")
s1$results()$serlabel    # =Serieslabel

attributes(labeled[[c(1,5,5)]])
s2<-getSeries(labeled,1,5,5)
s2$results()$explabel                #labeled experiment shows up in explabel
s2$results()$serlabel
s2$results()$swplabel
s2$sweeps$ser
s2$sweeps$Serieslabel2      #Seriueslabel2 is directly from tree attribute and therefore "cleaned"
s2$sweeps$ExperimentLabel
s2$sweeps$Stimulus

# Beispiel gelabeled mehrere Experimente:
inflarx<-get_treeinfo("non_package_files/Inflarx_E603.dat")   #example file
load("non_package_files/CURSORS")
res_inflarx<-get_all(inflarx, "hERG")     # get_all in this branch automatically extracts labels
library(ggplot2)
ggplot(res_inflarx, aes(y=peak, x=Trace_Time)) + geom_point(aes(color=explabel))


con<-file("non_package_files/Inflarx_E603.dat", "rb")
attr(inflarx[[c(1,1)]], "dataptr")  %>% ephys2:::readAny(con,116, "int", 4)  # 603   ->GrExperimentNumber
attr(inflarx[[c(1,2)]], "dataptr")  %>% ephys2:::readAny(con,116, "int", 4)  # 602   ->GrExperimentNumber
attr(inflarx[[c(1,3)]], "dataptr")  %>% ephys2:::readAny(con,116, "int", 4)  # 600   ->GrExperimentNumber
close(con)

vg_bl<-reorder_tree(get_treeinfo("non_package_files/VG_Blocker.dat"), 1:8)
#showtree(vg_bl)

s117<-getSeries(vg_bl, 1,1,7)
init_cursor("NaIV", "min", curMin_, c(0.01,0.015))  # existing settings from previous sessions will not be overwritten
init_cursor("NaIV", "base", curMean_, c(0.005,0.009)) 
init_cursor("NaPharm", "base", curMean_, c(0.005,0.009)) 
init_cursor("NaPharm", "min", curMin_, c(0.01,0.015)) 
make_stimulus("NaIV", "voltage", -80, 10)
s117$PLXLS_set_file("non_package_files/VG_Blocker_1.xls")
s117$results()[,c("Compound", "Concentration", "swplabel")]   

plot(min~relTime, s117$results())
points(min~relTime, subset(s117$results(), !swplabel==""), pch=19)
text(min~relTime, labels=swplabel, s117$results(), srt=90, cex=0.7, pos=2)

# finally something useful!

# new itplot function
new_itplot<-function(series, cursorname){
  results=series$results()
  drcresults<-get_lpresults2(results, cursor = cursorname)
  geom_mark_lpends<-function(data) geom_errorbarh(data=data, aes(y=res,x=relTime.start, xmin=relTime.start, xmax=relTime.end, height=0.02))
  
  p<-ggplot(results, aes_string(y=cursorname, x="relTime")) + theme_bw() +
    geom_mark_lpends(data=drcresults) +
    geom_point(pch="o") + 
    geom_text( aes(label=swplabel), size=3, hjust=-.1, col="blue") + 
    ggtitle(series$sweeps$CompoundName, subtitle = sprintf("%s ~  %s ~  %s", series$sweeps$file, series$sweeps$exp, series$sweeps$ser))
  print(p)
  drcresults
}


# see how easy it is now:
library(ephys2)
library(magrittr)
library(ggplot2)
franzi<-get_treeinfo("non_package_files/20140424.dat")
init_cursor('hERG-drc','peak',curMax_,c(2.3212703220623,2.4618657208954))
init_cursor('hERG-drc','base',curMean_,c(0.18221175410099,0.22238186805331))
#showtree(franzi) #define cursors!
set_resultmethod("hERG-drc",name = "corr", function(x) {x$corr<-x$peak-x$base;x})

write_explabel(franzi, 1,2,"Quinidine (experiment label)")
write_swplabel(franzi, 1,2,2,25, "0.2")  # set a new concentration
write_swplabel(franzi, 1,2,2,40, "just a comment")  # if it does not contain a numeric, its just a comment

franzi<-get_treeinfo("non_package_files/20140424.dat")  # right now we have to re-read the tree to get the updated labels! (labels still taken from tree)
#getSeries(franzi, 1,2,2)->s;s$results()[23,]
fr1<-getSeries(franzi, 1,1,2                                        )  %>% new_itplot("corr")
fr2<-getSeries(franzi, 1,2,2                                        )  %>% new_itplot("corr")
fr3<-getSeries(franzi, 1,3,2, CompoundName = "Quinidine (parameter)" )  %>% new_itplot("corr")

drc_(rbind(fr1, fr2,fr3), xlim=c(0.001,1000))
tdanker/ephys2 documentation built on Aug. 11, 2019, 12:12 p.m.